import { ERouteKey, ERoutePath } from ':/routes/defined';
import { useCallback } from 'react';
import { useNavigate } from 'react-router';
import { useSearchParams } from 'react-router-dom';

export function useBack() {
  const navigate = useNavigate();
  return useCallback(() => {
    navigate(-1);
  }, [navigate]);
}

export function useRedirect() {
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  return useCallback((replace: boolean = true) => {
    navigate(searchParams.get(ERouteKey.REDIRECT) || ERoutePath.HOME, { replace });
  }, [navigate, searchParams]);
}

export function useNavToHome() {
  const navigate = useNavigate();
  return useCallback(() => {
    navigate(ERoutePath.HOME);
  }, [navigate]);
}